import collections
class Solution(object):
    def areConnected(self, n, threshold, queries):
        m = len(queries)
        if threshold == 0:
            return [True] * m
        d = collections.defaultdict(set)
        for i in range(1, n + 1):
            for value in range(1, int(i ** 0.5) + 2):
                if i % value == 0:
                    if value > threshold:
                        d[value].add(i)
                    if i // value > threshold:
                        d[i // value].add(i)
        p = [i for i in range(n)]
        
        def find(x):
            if p[x] == x:
                return p[x]
            else:
                p[x] = find(p[x])
                return p[x]
        
        def union(x, y):
            rx = find(x)
            ry = find(y)
            if rx != ry:
                if rx < ry:
                    p[ry] = rx
                else:
                    p[rx] = ry
        
        for a_list in d.values():
            a_list = list(a_list)
            t = len(a_list)
            for i in range(1, t):
                union(a_list[0] - 1, a_list[i] - 1)
        return [find(u - 1) == find(v - 1) for u, v in queries]

data = Solution()
n = 6
threshold = 2
queries = [[1,4],[2,5],[3,6]]
print(data.areConnected(n, threshold, queries))